#include "Scene.h"

Scene::Scene(void)
{
	this->direction = 1;
	this->hasTurned = false;
}

Scene::~Scene(void)
{
	
}

HRESULT Scene::Init(int level, CZenKeyboard *Keyboard)
{
	HRESULT res;
	res = m_GameFieldFrame.Init(level, Keyboard);
	if (FAILED(res))
	{
		return E_FAIL;
	}	

	return S_OK;
}

HRESULT Scene::Render()
{
	HRESULT res;

	res = m_GameFieldFrame.Render();
	if (FAILED(res))
	{
		return E_FAIL;
	}	

	return S_OK;
}

void Scene::HandleKeyInput(CZenKeyboard * keyboard)
{
	float temp_x, temp_y, temp_z;
	m_GameFieldFrame.m_pacman.m_Frame1.GetPosition(temp_x, temp_z, temp_y);

	if(m_GameFieldFrame.wolfenstein3dMode)
		{
		if(this->direction == 1)
			{
			if(keyboard->IsKeyDown(DIK_UP))
				{
					if(this->hasTurned == false)
						{
						if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.1))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.9))!=1)
							{
								this->direction = 1;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}
				else if(keyboard->IsKeyDown(DIK_DOWN))
				{
					if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.9))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.1))!=1)
							{
								this->direction = 2;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}
				else if(keyboard->IsKeyDown(DIK_LEFT))
				{
					if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x-0.5))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x-0.5))!=1)
							{
								this->direction = 3;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}
				else if(keyboard->IsKeyDown(DIK_RIGHT))
				{
					if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x+1.5))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x+1.5))!=1)
							{
								this->direction = 4;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}	
				else if(!keyboard->IsKeyDown(DIK_UP))
				{
					this->hasTurned = false;
				}	
				else if(!keyboard->IsKeyDown(DIK_LEFT))
				{
					this->hasTurned = false;
				}
				else if(!keyboard->IsKeyDown(DIK_RIGHT))
				{
					this->hasTurned = false;
				}
				else if(!keyboard->IsKeyDown(DIK_DOWN))
				{
					this->hasTurned = false;
				}						
			}
			else if(this->direction == 2)
			{
			if(keyboard->IsKeyDown(DIK_UP))
				{
				if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.9))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.1))!=1)
							{
								this->direction = 2;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}	
				}
				else if(keyboard->IsKeyDown(DIK_DOWN))
				{
				if(this->hasTurned == false)
						{
						if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.1))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.9))!=1)
							{
								this->direction = 1;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}
				else if(keyboard->IsKeyDown(DIK_LEFT))
				{
					if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x+1.5))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x+1.5))!=1)
							{
								this->direction = 4;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}
				}
				else if(keyboard->IsKeyDown(DIK_RIGHT))
				{
					if(this->hasTurned == false)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x-0.5))!=1)
						{
							if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x-0.5))!=1)
							{
								this->direction = 3;
								m_GameFieldFrame.m_pacman.Turn(this->direction);
								this->hasTurned = true;
							}
						}
					}				
				}	
				else if(!keyboard->IsKeyDown(DIK_UP))
				{
					this->hasTurned = false;
				}	
				else if(!keyboard->IsKeyDown(DIK_LEFT))
				{
					this->hasTurned = false;
				}
				else if(!keyboard->IsKeyDown(DIK_RIGHT))
				{
					this->hasTurned = false;
				}
				else if(!keyboard->IsKeyDown(DIK_DOWN))
				{
					this->hasTurned = false;
				}
			}
		else if(this->direction == 3)
			{
			if(keyboard->IsKeyDown(DIK_UP))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x-0.5))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x-0.5))!=1)
						{
							this->direction = 3;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}				
			}
			else if(keyboard->IsKeyDown(DIK_DOWN))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x+1.5))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x+1.5))!=1)
						{
							this->direction = 4;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}
			}
			else if(keyboard->IsKeyDown(DIK_LEFT))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.9))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.1))!=1)
						{
							this->direction = 2;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}
			}
			else if(keyboard->IsKeyDown(DIK_RIGHT))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.1))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.9))!=1)
						{
							this->direction = 1;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}
			}	
			else if(!keyboard->IsKeyDown(DIK_UP))
			{
				this->hasTurned = false;
			}	
			else if(!keyboard->IsKeyDown(DIK_LEFT))
			{
				this->hasTurned = false;
			}
			else if(!keyboard->IsKeyDown(DIK_RIGHT))
			{
				this->hasTurned = false;
			}
			else if(!keyboard->IsKeyDown(DIK_DOWN))
			{
				this->hasTurned = false;
			}
		}
		else if(this->direction == 4)
			{
			if(keyboard->IsKeyDown(DIK_UP))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x+1.5))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x+1.5))!=1)
						{
							this->direction = 4;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}
			}
			else if(keyboard->IsKeyDown(DIK_DOWN))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x-0.5))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x-0.5))!=1)
						{
							this->direction = 3;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}	
			}
			else if(keyboard->IsKeyDown(DIK_LEFT))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.1))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.9))!=1)
						{
							this->direction = 1;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}
			}
			else if(keyboard->IsKeyDown(DIK_RIGHT))
			{
				if(this->hasTurned == false)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.9))!=1)
					{
						if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.1))!=1)
						{
							this->direction = 2;
							m_GameFieldFrame.m_pacman.Turn(this->direction);
							this->hasTurned = true;
						}
					}
				}					
			}	
			else if(!keyboard->IsKeyDown(DIK_UP))
			{
				this->hasTurned = false;
			}	
			else if(!keyboard->IsKeyDown(DIK_LEFT))
			{
				this->hasTurned = false;
			}
			else if(!keyboard->IsKeyDown(DIK_RIGHT))
			{
				this->hasTurned = false;
			}
			else if(!keyboard->IsKeyDown(DIK_DOWN))
			{
				this->hasTurned = false;
			}
		}
		
	}
	else
	{
		if(keyboard->IsKeyDown(DIK_UP))
		{
			if(this->hasTurned == false)
				{
				if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.1))!=1)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+1.5)).at((int)(temp_x+0.9))!=1)
					{
						this->direction = 1;
						m_GameFieldFrame.m_pacman.Turn(this->direction);
						this->hasTurned = true;
					}
				}
			}
		}
		else if(keyboard->IsKeyDown(DIK_DOWN))
		{
			if(this->hasTurned == false)
			{
				if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.9))!=1)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y-0.5)).at((int)(temp_x+0.1))!=1)
					{
						this->direction = 2;
						m_GameFieldFrame.m_pacman.Turn(this->direction);
						this->hasTurned = true;
					}
				}
			}
		}
		else if(keyboard->IsKeyDown(DIK_LEFT))
		{
			if(this->hasTurned == false)
			{
				if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x-0.5))!=1)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x-0.5))!=1)
					{
						this->direction = 3;
						m_GameFieldFrame.m_pacman.Turn(this->direction);
						this->hasTurned = true;
					}
				}
			}
		}
		else if(keyboard->IsKeyDown(DIK_RIGHT))
		{
			if(this->hasTurned == false)
			{
				if(m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x+1.5))!=1)
				{
					if(m_GameFieldFrame.map.at((int)(temp_y+0.1)).at((int)(temp_x+1.5))!=1)
					{
						this->direction = 4;
						m_GameFieldFrame.m_pacman.Turn(this->direction);
						this->hasTurned = true;
					}
				}
			}
		}	
		else if(!keyboard->IsKeyDown(DIK_UP))
		{
			this->hasTurned = false;
		}	
		else if(!keyboard->IsKeyDown(DIK_LEFT))
		{
			this->hasTurned = false;
		}
		else if(!keyboard->IsKeyDown(DIK_RIGHT))
		{
			this->hasTurned = false;
		}
		else if(!keyboard->IsKeyDown(DIK_DOWN))
		{
			this->hasTurned = false;
		}
	}
}

int Scene::GetLength(void)
{
	return m_GameFieldFrame.GetLength();
}

int Scene::GetWidth(void)
{
	return m_GameFieldFrame.GetWidth();
}
